package com.astrob.TileSource;

import org.osmdroid.tileprovider.MapTile;
import org.osmdroid.tileprovider.tilesource.OnlineTileSourceBase;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by colin on 2017/10/10.
 * <p>
 * <provider id="6042" type="0" visible="true" background="6010">
 * <name>混合图</name>
 * <mode>谷歌卫星+谷歌道路</mode>
 * <countries>China</countries>
 * <url><![CDATA[http://127.0.0.1:25412/getnotfake.act?x={x}&y={y}&z={z}&size=256&output=png&url=http%3a%2f%2fmt0.google.cn%2fvt%2fimgtp%3dpng32%26lyrs%3dh%40207000000%26ahl%3dzh-CN%26gl%3dCN%26src%3dapp%26x%3d%7bx%7d%26y%3d%7by%7d%26z%3d%7bz%7d]]></url>
 * <serverPart>0;1;2;3</serverPart>
 * <zoomPart>{z}-8</zoomPart>
 * <zoomMin>8</zoomMin>
 * <zoomMax>27</zoomMax>
 * <tileSize>256</tileSize>
 * <attribution><![CDATA[谷歌混合GG-纠偏 Locus群3035043]]></attribution>
 * <extraHeader><![CDATA[User-Agent#Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1]]></extraHeader>
 * <extraHeader><![CDATA[Referer#http://maps.google.com/]]></extraHeader>
 * </provider>
 */

//GoogleMap图源
public class GoogleSRMapsTileSource extends OnlineTileSourceBase {
    /**
     * @param aName                a human-friendly name for this tile source  自定图源义名字，会在手机外部存储中新建以该名字命名的文件夹，瓦片存储在其中
     * @param aZoomMinLevel        the minimum zoom level this tile source can provide  最小缩放级别
     * @param aZoomMaxLevel        the maximum zoom level this tile source can provide  最大缩放级别
     * @param aTileSizePixels      the tile size in pixels this tile source provides  瓦片质量 （256）
     * @param aImageFilenameEnding the file name extension used when constructing the filename  瓦片格式（jpg[有损压缩率高、不透明]、png[无损、透明]）
     * @param aBaseUrl             the base url(s) of the tile server used when constructing the url to download the tiles  下载瓦片的链接（前缀）
     */
    public GoogleSRMapsTileSource(String aName, int aZoomMinLevel, int aZoomMaxLevel, int aTileSizePixels,
                                  String aImageFilenameEnding, String[] aBaseUrl) {
        super(aName, aZoomMinLevel, aZoomMaxLevel, aTileSizePixels, aImageFilenameEnding, aBaseUrl);
    }

    //http://mt0.google.cn/vt/imgtp=png32&lyrs=h@207000000&ahl=zh-CN&gl=CN&src=app&x={x}&y={y}&z={z}
    //http://mt0.google.cn/vt/imgtp=png32&lyrs=h@207000000&ahl=zh-CN&gl=CN&src=app&x=26978&y=12404&z=15
    //127.0.0.1:25412/getnotfake.act?&x=26977&y=12404&z=15&size=256&output=png&url=http%3A%2F%2Fmt0.google.cn%2Fvt%2Fimgtp%3Dpng32%26lyrs%3Dh%40207000000%26ahl%3Dzh-CN%26gl%3DCN%26src%3Dapp%26x%3D26977%26y%3D12404%26z%3D15
    @Override
    public String getTileURLString(MapTile aTile) {
        String url = "";
        if (_mapsTileSource2 != null) {
            try {
                url = _mapsTileSource2.getTileURLString(aTile);
                url = URLEncoder.encode(url, "utf-8");
            } catch (UnsupportedEncodingException e) {
            }
        }

        String s = getBaseUrl() + "&x=" + aTile.getX() + "&y=" + aTile.getY() + "&z=" + aTile.getZoomLevel()
                + "&size=256&output=png&url=" + url;
        return s;
    }

    private OnlineTileSourceBase _mapsTileSource2;

    public void setMapsTileSource2(OnlineTileSourceBase mapsTileSource2) {
        _mapsTileSource2 = mapsTileSource2;
    }
}